<?php

use app\modules\Admin\models\dianpufenhongrewardModel;
use app\modules\Admin\models\dianpuModel;
use Illuminate\Database\Capsule\Manager as DB;

#[店铺分红管理]
class DianpufenhongrewardController extends AcommonController {

    public function init() {
        $this->model = new dianpufenhongrewardModel();
        $this->table = 'dianpu_reward';
        $this->primaryKey = 'id';
        parent::init();
    }

    #[分红记录]
    public function indexAction($returnData = FALSE)
    {
        if($this->isPost()){
            $params = $this->postData;
            $page = $params['page'] ?? 1;
            $per_page = $params['rows'] ?? 10;

            $rewardModel = (new dianpufenhongrewardModel())->newQuery();
            $query = $rewardModel->with(['dianpuInfo','buyerInfo','orderInfo','orderItemInfo', 'dianpuManager'])->orderByDesc('created_at');
            if(isset($params['mode']) && $params['mode'] != 'all'){
                switch ($params['mode']){
                    case 'waiting':
                        $query = $query->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_WAITING);
                        break;
                    case 'accounted':
                        $query = $query->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
                        break;
                    case 'cancel':
                        $query = $query->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_CANCEL);
                        break;
                    case 'back':
                        $query = $query->where('status', tuikerewardModel::COMMISSION_REWARD_STATUS_BACK);
                        break;
                }
            }
            if(isset($params['start_on']) && !empty($params['start_on'])){
                $query= $query->whereDate('created_at','>=', $params['start_on']);
            }
            if(isset($params['end_on']) && !empty($params['end_on'])){
                $query= $query->whereDate('created_at', '<=', $params['end_on']);
            }
            if(isset($params['keywords']) && !empty($params['keywords'])){
                if(isset($params['keywordsType']) && !empty($params['keywordsType'])){
                    switch ($params['keywordsType']){
                        case 'dianpu_id':
                            $query = $query->where('dianpu_id', $params['keywords']);
                            break;
                        case 'dianpu_phone':
                            $query = $query->whereIn('dianpu_id', dianpuModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'manager_id':
                            $query = $query->where('manager_id', $params['keywords']);
                            break;
                        case 'manager_phone':
                            $query = $query->whereIn('manager_phone', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'order_id':
                            $query = $query->where('order_id', $params['keywords']);
                            break;
                        case 'order_sn':
                            $query = $query->whereIn('order_id', orderModel::query()->where('order_sn', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                        case 'buyer_id':
                            $query = $query->where('buyer_id', $params['keywords']);
                            break;
                        case 'buyer_phone':
                            $query = $query->whereIn('buyer_id', memberModel::query()->where('phone', 'like', "%{$params['keywords']}%")->pluck('id')->toArray());
                            break;
                    }
                }
            }
            $total = $query->count();
            if(isset($params['sort'])){
                $query = $query->orderBy($params['sort']?:'id', (isset($params['order'])&&in_array($params['order'],['asc','desc']))?$params['order']:'desc');
            }
            $data = $query->offset(($page - 1) * $per_page)
                          ->limit($per_page)
                          ->get()->toArray();
            json([
                     'total' => $total,
                     'rows'=>$data
                 ]);
        }else{
            $this->_view->display( strtolower($this->controller) . '/index.html');
        }
    }
}
